#pragma once

#include <string>
#include <Windows.h>

inline std::wstring UTF8toUTF16(const char* p, size_t count)
{
	if (count <= 0)
		return std::wstring();
	std::wstring utf16(count, 0);
	utf16.resize(MultiByteToWideChar(CP_UTF8, 0, p, count, &utf16.front(), utf16.size()));
	return std::move(utf16);
}

inline std::wstring UTF8toUTF16(const char* p)
{
	return UTF8toUTF16(p, strlen(p));
}

inline std::wstring UTF8toUTF16(const std::string& str)
{
	return UTF8toUTF16(str.c_str(), str.size());
}

inline std::string UTF16toUTF8(const wchar_t* p, size_t count)
{
	if (count <= 0)
		return std::string();
	std::string utf8(count * 3, 0);
	utf8.resize(WideCharToMultiByte(CP_UTF8, 0, p, count, &utf8.front(), utf8.size(), NULL, NULL));
	return std::move(utf8);
}

inline std::string UTF16toUTF8(const wchar_t* p)
{
	return UTF16toUTF8(p, wcslen(p));
}

inline std::string UTF16toUTF8(const std::wstring& str)
{
	return UTF16toUTF8(str.c_str(), str.size());
}

inline bool EndWith(const std::wstring& str, const wchar_t* pSubstr, size_t count)
{
	if (str.size() >= count)
	{
		const wchar_t* p = &*(str.end() - count);
		if (wcsncmp(p, pSubstr, count) == 0)
			return true;
	}
	return false;
}

inline bool EndWithI(const std::wstring& str, const wchar_t* pSubstr, size_t count)
{
	if (str.size() >= count)
	{
		const wchar_t* p = &*(str.end() - count);
		if (_wcsnicmp(p, pSubstr, count) == 0)
			return true;
	}
	return false;
}
